﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;
using Microsoft.AspNet.Identity;

namespace CallCenter.Web.Controllers
{
    [AuthorizeCore]
    [Authorize]
    public class ScratchPadController : BaseController
    {
        // GET: ScratchPad
        public ActionResult Index()
        {

            return View();
        }

        public ActionResult ScratchHistory()
        {
            ScratchPadViewModel scrhPad = new ScratchPadViewModel();
            return View(scrhPad);
        }
      
        public JsonResult GetScratchHistory()
        {

            var scratchPads = this.Bridge.ScratchUtility.GetHistoryList(this.Bridge.AccountUtility.GetUserIDByUserNames(User.Identity.Name.ToLower()));
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult CreateScratchHistory(ScratchPadViewModel model)
        {
          
            int userId = 0;
      
            if (string.IsNullOrEmpty(model.contactname))
            {
                model.ContactId = this.Bridge.ccContactUtility.GetContactIdByScName(model.Name);
                userId = this.Bridge.AccountUtility.GetUserIDByUserNames(User.Identity.Name.ToLower());
            }
            else
            {
                model.ContactId = this.Bridge.ccContactUtility.GetContactIdByScName(model.contactname);
                try
                {
                    var ContactDetail = this.Bridge.ccContactUtility.GetContactDetailByContactId(model.ContactId);
                    userId = ContactDetail.userId;
                }
                catch
                {
                    userId = 0;
                }
            }

                model.productid = this.Bridge.ProductsUtility.GetProductIdByName(model.product);
                model.companyid = this.Bridge.CompanyUtility.GetCompanyIdByName(model.company);
                model.status = this.Bridge.StatusUtility.GetStatusIdByName(model.statusName);
                model.viaId = this.Bridge.CommunicationMediumUtility.GetCommunicationMediumIdByName(model.commVia);
           
           model.userId = userId;
            var returnMessage = this.Bridge.ScratchUtility.InsertToCallLog(model);
            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else {
                return Json(new AjaxSuccess { Message = "Record has been succesfully Inserted" }, JsonRequestBehavior.AllowGet);
            }
     
            return Json(returnMessage, JsonRequestBehavior.AllowGet);
          
        }

        [HttpPost]
        public JsonResult DeleteScratchHistory(ScratchPadViewModel model)
        {
          
            int id = model.clid;
            this.Bridge.ScratchUtility.Delete(id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateScratchHistory(ScratchPadViewModel model)
        {
            int userId = 0;
            model.loggedOwner = User.Identity.Name;
            if (string.IsNullOrEmpty(model.contactname))
            {
                model.ContactId = this.Bridge.ccContactUtility.GetContactIdByScName(model.Name);
                userId = this.Bridge.AccountUtility.GetUserIDByUserNames(User.Identity.Name.ToLower());
            }
            else
            {
                model.ContactId = this.Bridge.ccContactUtility.GetContactIdByScName(model.contactname);
                try
                {
                    var ContactDetail = this.Bridge.ccContactUtility.GetContactDetailByContactId(model.ContactId);
                    userId = ContactDetail.userId;
                }
                catch
                {
                    userId = 0;
                }
            }

          // Get  id based on selected dropdown value
            model.productid = this.Bridge.ProductsUtility.GetProductIdByName(model.product);
            model.companyid = this.Bridge.CompanyUtility.GetCompanyIdByName(model.company);
            model.status = this.Bridge.StatusUtility.GetStatusIdByName(model.statusName);
            model.viaId = this.Bridge.CommunicationMediumUtility.GetCommunicationMediumIdByName(model.commVia);          
            model.userId = userId;
            var result = this.Bridge.ScratchUtility.UpdateCallLog(model);
            return Json(result, JsonRequestBehavior.AllowGet);
        }    
        public ActionResult GetJustNameContact()
        {
            var scratchPads = this.Bridge.ccContactUtility.GetJustName();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetJustCompanyName()
        {
            var scratchPads = this.Bridge.CompanyUtility.GetJustName();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetLookUpCompanyName()
        {
            var scratchPads = this.Bridge.CompanyUtility.GetLookUpCompany();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetAllProducts()
        {
            var scratchPads = this.Bridge.ProductsUtility.GetAll();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetAllCommunicationMedium()
        {
            var scratchPads = this.Bridge.CommunicationMediumUtility.GetAll();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetStatus()
        {
            var scratchPads = this.Bridge.StatusUtility.GetAll();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetContactDetail()
        {
            var scratchPads = this.Bridge.ccContactUtility.GetContactSDetails();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetCallerDetail()
        {
            var scratchPads = this.Bridge.ccContactUtility.GetCallerDetails();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }



        public ActionResult GetPhoneNumbers()
        {

            var scratchPads = this.Bridge.ScratchUtility.GetPhoneNumbers();

            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }
    }
}